<template>
  <el-dialog 
    v-model="dialogVisible" 
    :title="dialogTitle" 
    width="50%" 
    :before-close="handleClose">
    <el-form 
      ref="ruleFormRef" 
      :model="formData" 
      :rules="rules" 
      label-width="120px" 
      class="demo-ruleForm">
      <el-form-item label="品牌名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="LOGO上传">
        <el-upload
          class="avatar-uploader"
          action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm(ruleFormRef, dialogTitle)">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { Plus } from '@element-plus/icons-vue';
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage } from 'element-plus';
import type { UploadProps } from 'element-plus'
import { brandAddList } from "../../../../api/product/brand/index"


const imageUrl = ref('')
const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
  uploadFile
) => {
  console.log(response)
  imageUrl.value = URL.createObjectURL(uploadFile.raw!)
}
// 上传图片组件---上传图片之前的钩子函数
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  console.log(rawFile.type,2)
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('1111')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}

interface RuleForm {
  name: string,
  logoUrl: string,
}
const dialogVisible = ref(false);
const ruleFormRef = ref()
const formData = reactive<RuleForm>({
  name: "",
  logoUrl: ""
})
const rules = reactive<FormRules<RuleForm>>({
  name: [{ required: true, message: '请输入品牌名称', trigger: 'blur' }],
})
//保存
const confirm = async(formEl: FormInstance | undefined, key: any)=> {
  if (!formEl) return;
    await formEl.validate(async (valid) => {
      if (valid) {
        if (key == "新增") {
          let data = (await brandAddList(formData)) as any;
          if(data.code == 200) {
            dialogVisible.value = false;
          }
        } else {
        }
      }
    });
  
}
//关闭窗口
const handleClose = () => {
  dialogVisible.value = false;
}
const receiveData = (row:any) => {
 console.log(row,2)
 formData.name = row.name;
}
//向外暴漏
defineExpose({
  dialogVisible,
  receiveData
})
defineProps({
  dialogTitle: String
})
</script>
<style lang="less">
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>